package com.yeehot.iot_manager;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeehot.iot_manager.bean.Device;
import com.yeehot.iot_manager.ui.login.LoginActivity;
import com.yeehot.iot_manager.utils.INetCallBack;
import com.yeehot.iot_manager.utils.OkHttpUtils;

public class DetailActivity extends AppCompatActivity {

    private  String deviceName="";

    private Switch light1;

    private Switch light2;

    private Button getLight1;
    private Button getLight2;
    private EditText tmp;

    private  TextView title;
    private TextView tmpdetail;
    private Button getTmp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        light1=findViewById(R.id.switch1);

        light2=findViewById(R.id.switch2);
        tmpdetail=findViewById(R.id.tmpdetail);

        tmp=findViewById(R.id.tmp_edit);
        getTmp=findViewById(R.id.settmp);
        title=findViewById(R.id.device);


        getTmp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("consumerAccount", LoginActivity.userName);
                jsonObject.put("identifier","Temperature");
                jsonObject.put("value",Float.parseFloat(tmp.getText().toString()));
                jsonObject.put("deviceName",deviceName);
                jsonObject.put("limit",100);

                setData(jsonObject);
            }
        });
        light1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                int value=0;
                if (isChecked){
                    //选中状态 可以做一些操作
                    value=1;
                }else {
                    //未选中状态 可以做一些操作
                    value=0;
                }
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("consumerAccount", LoginActivity.userName);
                jsonObject.put("identifier","LightSwitch");
                jsonObject.put("value",value);
                jsonObject.put("deviceName",deviceName);
                jsonObject.put("limit",100);

                setData(jsonObject);
            }
        });
        light2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int value=0;
                if (isChecked){
                    //选中状态 可以做一些操作
                    value=1;
                }else {
                    //未选中状态 可以做一些操作
                    value=0;
                }
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("consumerAccount", LoginActivity.userName);
                jsonObject.put("identifier","NightLightSwitch");
                jsonObject.put("value",value);
                jsonObject.put("deviceName",deviceName);
                jsonObject.put("limit",100);

                setData(jsonObject);
            }
        });
        Bundle bundle=getIntent().getExtras();

        deviceName=bundle.getString("deviceName");
        title.setText(deviceName);
        //这里所有的参数都是毫秒，使用秒需要 time乘以1000
        //long millisInFuture 倒计时总时间
        //long countDownInterval 间隔多久倒计时一次
        //以下参数是 一共60秒，每10秒倒计时一次
        handler.postDelayed(runnable, 1000);//每两秒执行一次runnable.
        getdata();
    }
    Handler handler=new Handler();
    Runnable runnable=new Runnable(){
        @Override
        public void run() {
// TODO Auto-generated method stub
//要做的事情

            getdata();
            handler.postDelayed(this, 3000);
        }
    };
    private void setData(JSONObject info) {


        Log.d("设备温度", "setData: "+info.toJSONString());
        OkHttpUtils.getInstance().doPostJson("command", info.toJSONString(), new INetCallBack() {
            @Override
            public void onSuccess(String response) {
                System.out.println(response+"===");
                JSONObject data=JSONObject.parseObject(response);
                if (data.getBoolean("success"))
                {


                    Toast.makeText(DetailActivity.this,"设置成功",Toast.LENGTH_LONG).show();


                }

            }

            @Override
            public void onFailed(Throwable ex) {

                Toast.makeText(DetailActivity.this,"网络错误",Toast.LENGTH_LONG).show();
            }
        });

    }
    private void getdata() {

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("consumerAccount", LoginActivity.userName);
        jsonObject.put("deviceName",deviceName);
        jsonObject.put("limit",100);

        OkHttpUtils.getInstance().doPostJson("deviceDetail", jsonObject.toJSONString(), new INetCallBack() {
            @Override
            public void onSuccess(String response) {

                JSONObject data=JSONObject.parseObject(response);
                if (data.getBoolean("success"))
                {


                    JSONObject info=data.getJSONObject("data");

                    boolean light01_status=info.getBoolean("LightSwitch");
                    boolean light02_status=info.getBoolean("NightLightSwitch");
                    float tmperature=info.getFloat("Temperature");


                    if (light01_status){
                        light1.setChecked(true);

                    }
                    else{
                        light1.setChecked(false);
                    }
                    if (light02_status){
                        light2.setChecked(true);

                    }
                    else{
                        light2.setChecked(false);
                    }
                    tmpdetail.setText(tmperature+"");

                    System.out.println(response+"===");
                }

            }

            @Override
            public void onFailed(Throwable ex) {

                Toast.makeText(DetailActivity.this,"网络错误",Toast.LENGTH_LONG).show();
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();


    }


}